package jwlee;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JFrame;

public class SinDrawer {

	public static void main(String[] args) {
		final JFrame f = new JFrame("draw sin");
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		f.setSize(d);
		f.setVisible(true);
		final Image oimg = f.createImage(d.width, d.height);
		final Graphics og = oimg.getGraphics();

		class MyCanvas extends Canvas implements Runnable {

			private boolean stop = false, firstrun = true;

			int originX = 100, originY = 300;// 坐标原点

			int coordinateWidth = 600, coordinateHeight = 200;// 坐标宽和高

			int x = originX, y = originY;

			Random r = new Random();

			public MyCanvas() {
				new Thread(this).start();
			}

			public void run() {
				while (!stop) {
					repaint();

					// 控制绘制速度
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					if ((x - originX) > 600)
						stop = true;
				}
			}

			public void paint(Graphics g) {
				if (oimg != null) {
					g.drawImage(oimg, 0, 0, this);
				}
				if (firstrun) {// 第一次运行时绘坐标
					// 原点:
					g.drawString("0", originX - 10, originY + 10);
					// 横坐标:
					g.drawLine(originX, originY, originX + coordinateWidth, originY);
					g.drawLine(originX + coordinateWidth - 10, originY + 5, originX
							+ coordinateWidth, originY);
					g.drawLine(originX + coordinateWidth - 10, originY - 5, originX
							+ coordinateWidth, originY);
					// 纵坐标:
					g.drawLine(originX, originY, originX - 5, originY - coordinateHeight);
					g.drawLine(originX - 5, originY - coordinateHeight + 10, originX, originY
							- coordinateHeight);
					g.drawLine(originX + 5, originY - coordinateHeight + 10, originX, originY
							- coordinateHeight);

					og.drawString("0", originX - 10, originY + 10);
					og.drawLine(originX, originY, originX + coordinateWidth, originY);
					og.drawLine(originX + coordinateWidth - 10, originY + 5, originX
							+ coordinateWidth, originY);
					og.drawLine(originX + coordinateWidth - 10, originY - 5, originX
							+ coordinateWidth, originY);
					og.drawLine(originX, originY, originX, originY - coordinateHeight);
					og.drawLine(originX - 5, originY - coordinateHeight + 10, originX, originY
							- coordinateHeight);
					og.drawLine(originX + 5, originY - coordinateHeight + 10, originX, originY
							- coordinateHeight);
					firstrun = false;
				}
				// g.drawRect(0,0,100,100);
				// g.fill3DRect(0,0,50,50,true);
				g.setColor(Color.RED);
				og.setColor(Color.RED);
				int k = r.nextInt(2);
				k--;
				// int newx = x + 5;
				// int newy = y + k;
				int newx = x + 1;
				int newy = (int) (200 + 100 * Math.sin(Math.PI / 40 * (newx)));
				if ((x - originX) > 400 && (x - originX) < 600) {
					newy = (int) (200 + 100 * Math.sin(Math.PI / 40 * (newx)) * (1 + Math.random())
							/ 2);
				}
				g.drawLine(x, y, newx, newy);
				og.drawLine(x, y, newx, newy);
				x = newx;
				y = newy;
				// g.drawOval(x,y,10,10);
				// g.fillOval(x,y,10,10);
				// g.drawString(".",i,j);
				// if(i>100)stop=true;
			}

			public void update(Graphics g) {
				paint(g);
			}
		}
		f.getContentPane().add(new MyCanvas());
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
}